@echo off
setlocal enabledelayedexpansion

REM FTP服务器测试脚本 (Windows版本)
echo 🧪 FTP服务器测试脚本
echo ====================

REM 设置颜色 (Windows 10+)
for /f %%i in ('echo prompt $E ^| cmd') do set "ESC=%%i"
set "GREEN=%ESC%[32m"
set "RED=%ESC%[31m"
set "YELLOW=%ESC%[33m"
set "NC=%ESC%[0m"

REM 函数：打印成功消息
:print_success
echo %GREEN%✅ %~1%NC%
goto :eof

REM 函数：打印错误消息
:print_error
echo %RED%❌ %~1%NC%
goto :eof

REM 函数：打印警告消息
:print_warning
echo %YELLOW%⚠️ %~1%NC%
goto :eof

REM 函数：打印信息消息
:print_info
echo %YELLOW%ℹ️ %~1%NC%
goto :eof

REM 检查Rust是否安装
:check_rust
call :print_info "检查Rust环境..."
where cargo >nul 2>&1
if %errorlevel% equ 0 (
    call :print_success "Rust环境正常"
    cargo --version
) else (
    call :print_error "Rust未安装，请先安装Rust"
    exit /b 1
)
goto :eof

REM 构建项目
:build_project
call :print_info "构建FTP项目..."
cargo build --workspace
if %errorlevel% equ 0 (
    call :print_success "项目构建成功"
) else (
    call :print_error "项目构建失败"
    exit /b 1
)
goto :eof

REM 初始化测试数据
:init_test_data
call :print_info "初始化测试数据..."
cargo run --bin init_test_data
if %errorlevel% equ 0 (
    call :print_success "测试数据初始化完成"
) else (
    call :print_warning "测试数据初始化失败，但继续测试"
)
goto :eof

REM 运行单元测试
:run_unit_tests
call :print_info "运行单元测试..."
cargo test --workspace
if %errorlevel% equ 0 (
    call :print_success "单元测试通过"
) else (
    call :print_error "单元测试失败"
    exit /b 1
)
goto :eof

REM 运行集成测试
:run_integration_tests
call :print_info "运行集成测试..."
cargo run --bin integration_test
if %errorlevel% equ 0 (
    call :print_success "集成测试通过"
) else (
    call :print_error "集成测试失败"
    exit /b 1
)
goto :eof

REM 手动测试FTP服务器
:manual_test
call :print_info "手动测试FTP服务器..."
call :print_info "启动FTP服务器 (端口: 2121)..."

REM 在后台启动服务器
start /b cargo run --bin ftp-server -- --port 2121

REM 等待服务器启动
timeout /t 3 /nobreak >nul

call :print_info "服务器已启动"
call :print_info "可以使用以下命令测试："
echo   ftp localhost 2121
echo   用户名: testuser, 密码: testpass
echo   用户名: admin, 密码: adminpass
echo   用户名: readonly, 密码: readonlypass

call :print_info "按任意键停止服务器..."
pause >nul

REM 停止服务器 (简化处理)
taskkill /f /im ftp-server.exe >nul 2>&1
call :print_success "服务器已停止"
goto :eof

REM 清理测试数据
:cleanup
call :print_info "清理测试数据..."
if exist data rmdir /s /q data
call :print_success "测试数据已清理"
goto :eof

REM 显示帮助信息
:show_help
echo 用法: %~nx0 [选项]
echo.
echo 选项:
echo   build      - 构建项目
echo   init       - 初始化测试数据
echo   test       - 运行所有测试
echo   unit       - 运行单元测试
echo   integration - 运行集成测试
echo   manual     - 手动测试FTP服务器
echo   cleanup    - 清理测试数据
echo   help       - 显示此帮助信息
echo.
echo 示例:
echo   %~nx0 build    # 构建项目
echo   %~nx0 test     # 运行所有测试
echo   %~nx0 manual   # 手动测试服务器
goto :eof

REM 主函数
:main
if "%1"=="" set "1=test"
if "%1"=="build" (
    call :check_rust
    call :build_project
) else if "%1"=="init" (
    call :check_rust
    call :init_test_data
) else if "%1"=="test" (
    call :check_rust
    call :build_project
    call :init_test_data
    call :run_unit_tests
    call :run_integration_tests
) else if "%1"=="unit" (
    call :check_rust
    call :run_unit_tests
) else if "%1"=="integration" (
    call :check_rust
    call :run_integration_tests
) else if "%1"=="manual" (
    call :check_rust
    call :build_project
    call :init_test_data
    call :manual_test
) else if "%1"=="cleanup" (
    call :cleanup
) else if "%1"=="help" (
    call :show_help
) else (
    call :print_error "未知选项: %1"
    call :show_help
    exit /b 1
)
goto :eof

REM 运行主函数
call :main %*
